import { useModel } from "umi";
import { useReactive } from "ahooks";

import { FindAllObjects } from "../../_graphql/organization/object";

const useObjects = () => {
    const { graphqlFetchPlatform } = useModel("global");

    const state = useReactive({
        dataLoading: false,
        objects: [],
    });

    const loadObjects = async () => {
        state.dataLoading = true;
        let res = await graphqlFetchPlatform({
            query: FindAllObjects,
            variables: {},
        });
        state.objects = res?.a?.nodes || [];
        state.dataLoading = false;
    };

    return { ...state, loadObjects };
};

export default useObjects;
